package com.dikang.keer.product.service.impl;

import com.dikang.common.utils.PageUtils;
import com.dikang.common.utils.Query;
import com.dikang.keer.product.entity.BrandEntity;
import com.dikang.keer.product.entity.CategoryEntity;
import com.dikang.keer.product.service.BrandService;
import com.dikang.keer.product.service.CategoryService;
import com.dikang.keer.product.vo.CategoryBrandRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dikang.keer.product.dao.CategoryBrandRelationDao;
import com.dikang.keer.product.entity.CategoryBrandRelationEntity;
import com.dikang.keer.product.service.CategoryBrandRelationService;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private BrandService brandService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveCategoryBrandRelation(CategoryBrandRelationEntity categoryBrandRelation) {
        if (categoryBrandRelation.getBrandId() !=null && categoryBrandRelation.getCatelogId() !=null){
            BrandEntity brand = brandService.getById(categoryBrandRelation.getBrandId());
            CategoryEntity category = categoryService.getById(categoryBrandRelation.getCatelogId());
            categoryBrandRelation.setBrandName(brand.getName());
            categoryBrandRelation.setCatelogName(category.getName());

            this.save(categoryBrandRelation);

        }
    }

    @Override
    public List<CategoryBrandRelationVo> getCategoryBrandRelation(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntitys = baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<CategoryBrandRelationVo> collect = categoryBrandRelationEntitys.stream().map((item) -> {
            CategoryBrandRelationVo categoryBrandRelationVo = new CategoryBrandRelationVo();
            BeanUtils.copyProperties(item, categoryBrandRelationVo);
            return categoryBrandRelationVo;
        }).collect(Collectors.toList());
        return collect;
    }

}